derive_more
Rust has lots of builtin traits that are implemented for its basic types, such
as Add
, Not
, From
or Display
.
However, when wrapping these types inside your own structs or enums you lose the
implementations of these traits and are required to recreate them.
This is especially annoying when your own structures are very simple, such as
when using the commonly advised newtype pattern (e.g. MyInt(i32)
).
This library tries to remove these annoyances and the corresponding boilerplate code. It does this by allowing you to derive lots of commonly used traits for both structs and enums.
Example code
By using this library the following code just works:
use ;
;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
The derivable traits
Below are all the traits that you can derive using this library. Some trait derivations are so similar that the further documentation will only show a single one of them. You can recognize these by the "-like" suffix in their name. The trait name before that will be the only one that is used throughout the further documentation.
It is important to understand what code gets generated when using one of the derives from this crate. That is why the links below explain what code gets generated for a trait for each group from before.
You can use the cargo-expand
utility to see the exact code that is generated
for your specific type.
This will show you your code with all macros and derives expanded.
NOTE: You still have to derive each trait separately. So #[derive(Mul)]
doesn't
automatically derive Div
as well. To derive both you should do #[derive(Mul, Div)]
Conversion traits
These are traits that are used to convert automatically between types.
Formatting traits
These traits are used for converting a struct to a string in different ways.
Debug
Display
-like, containsDisplay
,Binary
,Octal
,LowerHex
,UpperHex
,LowerExp
,UpperExp
,Pointer
Error-handling traits
These traits are used to define error-types.
Operators
These are traits that can be used for operator overloading.
Index
Deref
Not
-like, containsNot
andNeg
Add
-like, containsAdd
,Sub
,BitAnd
,BitOr
,BitXor
Mul
-like, containsMul
,Div
,Rem
,Shr
andShl
Sum
-like, containsSum
andProduct
IndexMut
DerefMut
AddAssign
-like, containsAddAssign
,SubAssign
,BitAndAssign
,BitOrAssign
andBitXorAssign
MulAssign
-like, containsMulAssign
,DivAssign
,RemAssign
,ShrAssign
andShlAssign
Static methods
These don't derive traits, but derive static methods instead.
Constructor
, this derives anew
method that can be used as a constructor. This is very basic if you need more customization for your constructor, check out thederive-new
crate.IsVariant
, for each variantfoo
of an enum type, derives ais_foo
method.Unwrap
, for each variantfoo
of an enum type, derives anunwrap_foo
method.TryUnwrap
, for each variantfoo
of an enum type, derives antry_unwrap_foo
method.
Re-exports
This crate also re-exports all the standard library traits that it adds derives
for. So, both the Display
derive and the Display
trait will be in scope when
you add the following code:
use Display; // also imports `core::fmt::Display`
For derive macros only, without the corresponding traits, do import them from
the derive
module:
use Display; // imports macro only
Hygiene
For hygiene purposes, macros use derive_more::*
absolute paths in their expansions.
This might introduce a trouble, if you want to re-export derive_more
macros in your
own crate without using the derive_more
as a direct dependency in downstream crates:
use Display; // re-exported in `my_lib` crate
// error: could not find `derive_more` in the list of imported crates
;
In such case, you should re-export the derive_more
module too:
use ; // re-exported in `my_lib` crate
// works fine now!
;
Installation
To avoid redundant compilation times, by default no derives are supported.
You have to enable each type of derive as a feature in Cargo.toml
:
[]
# You can specify the types of derives that you need for less time spent
# compiling. For the full list of features see this crate its `Cargo.toml`.
= { = "1", = ["from", "add", "iterator"] }
[]
# If you don't care much about compilation times and simply want to have
# support for all the possible derives, you can use the "full" feature.
= { = "1", = ["full"] }
[]
# If you run in a `no_std` environment you should disable the default features,
# because the only default feature is the "std" feature.
# NOTE: You can combine this with "full" feature to get support for all the
# possible derives in a `no_std` environment.
= { = "1", = false }
And this to the top of your Rust file:
// use the derives that you want in the file
use ;
If you're still using Rust 2015, add this instead:
extern crate core;
extern crate derive_more;
# // omit wrapping statements above into `main()` in tests
MSRV policy
This library requires Rust 1.75 or higher.
Changing MSRV (minimum supported Rust version) of this crate is treated as a minor version change in terms of Semantic Versioning.
- So, if MSRV changes are NOT concerning for your project, just use the default caret requirement:
[] = "1" # or "1.0", or "^1.0"
- However, if MSRV changes are concerning for your project, then use the tilde requirement to pin to a specific minor version:
[] = "~1.0" # or "~1.0.0"